<template>
  <div class="login-container">
    <vue-particles
      class="login-bg"
      color="#dedede"
      :particle-opacity="0.7"
      :particles-number="80"
      shape-type="star"
      :particle-size="4"
      lines-color="#FFFFFF"
      :lines-width="2"
      :line-linked="true"
      :line-opacity="0.4"
      :lines-distance="150"
      :move-speed="3"
      :hover-effect="true"
      hover-mode="grab"
      :click-effect="true"
      click-mode="push"
    />

    <!--
        校验
        1、给form组件绑定model属性
        2、给form组件绑定rules
        3、给form-item绑定prop属性
        4、v-model双向绑定表单
       -->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <!-- 放置标题图片 @是设置的别名-->
      <div class="title-container">
        <h3 class="title">
          房地产后台管理系统
        </h3>
      </div>
      <el-form-item prop="identifier">
        <span class="svg-container">
          <!--
            svg-icon是在src/components中封装好的一个组件
            是专门用来渲染svg格式的图片的组件
            使用方式：
              给svg-icon标签添加icon-class属性并进行赋值
              这个值就是你想要渲染的那个svg图片的文件名
           -->
          <svg-icon icon-class="peoples" />
        </span>
        <el-input
          ref="identifier"
          v-model="loginForm.identifier"
          placeholder="请输入手机号码"
          name="identifier"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button

        class="loginBtn"
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">账号: admin</span>
        <span> 密码: adminadmin</span>
      </div>
      <div class="goRegister"><a href="#/register">没有账号？马上注册</a></div>
    </el-form>

  </div>
</template>

<script>
// import { login } from '@/api/login/index'
import { getUserInfo } from '@/api/user'
import { Message } from 'element-ui'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        identifier: 'admin',
        password: 'adminadmin'
      },
      loginRules: {
        identifier: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度必须6到16位', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {},
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    async handleLogin() {
      // await this.$refs.loginForm.validate()
      // await this.$store.dispatch('user/login', this.loginForm)
      // // console.log(res.data.jwt)
      // this.$router.push('/')
      // // const res = await getUserInfo()
      // // console.log(res)
      // this.$store.dispatch('user/getUserInfo')
      try {
        await this.$refs.loginForm.validate()
        Message.success('登录成功')
        await this.$store.dispatch('user/login', this.loginForm)
        // console.log(res.data.jwt)

        this.$router.push('/')
        this.$store.dispatch('user/getUserInfo')
        const res = await getUserInfo()
        console.log(res)
      } catch (error) {
        Message.error('自己的密码都记不住？')
      }
    }
  }
}
</script>

<style lang="scss">

// 全局样式 --- 用于重置elementUI组件的样式

/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:white;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  // 去注册
  .goRegister{
    text-decoration: none;
     color: orange;
    // text-align: center;
  }

  background-color: #283443;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
    // 解决浏览器中自动添加账号密码时 背景颜色覆盖的问题
    // 99999s 基本上就是一个无限长的时间
    // 通过延长增加自动填充背景色的方式, 是用户感受不到样式的变化
    input:-webkit-autofill,
    input:-webkit-autofill:hover,
    input:-webkit-autofill:focus,
    input:-webkit-autofill:active {
        transition-delay: 99999s;
        transition: color 99999s ease-out, background-color 99999s ease-out;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(111, 106, 106, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__error {
    color: #fff
  }
  .loginBtn {
    background: #407ffe;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
  }
}
</style>

<style lang="scss" scoped>
// 局部样式 --- 主要是实现设置当前组件的样式
$bg:#2d3a4b;
$dark_gray:black;
$light_gray:#eee;
.login-bg{
   width: 100%;
    height: 100%;
    background: #3E3E3E;
    position: absolute;
    background-image: url("../../assets/common/1.gif");
    // background-position: top center ;
    background-size: 100%;
    background-repeat:no-repeat ;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
